/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BD;

import BE.Cliente;
import BE.Descuentoxproducto;
import BE.Tipocliente;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Transaction;
import org.hibernate.Session;

/**
 *
 * @author dennis
 */
public class TipoClienteBD {

    private Session session;

    public TipoClienteBD() {

         this.session = null;

    }

    public List<Tipocliente> cargarTipoCliente() {
        List<Tipocliente> lista = null;
        Transaction tx =null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            String hql = "from Tipocliente order by srlTipocliente";
            Query query = session.createQuery(hql);
            session.flush();
            lista = query.list();
            tx.commit();

        } catch (Exception e) {
            e.printStackTrace();
            if(tx!=null)
            {
                tx.rollback();
            }
        }
        return lista;
    }

    public Tipocliente buscarTipoCliente(Cliente objClienteBE)
    {
        List<Tipocliente> lista = null;

        Transaction tx =null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            String consulta = "Select {tipocliente.*} " +
                        "From tipocliente " +
                        "where srl_tipocliente in " +
                        "(select srl_tipocliente from cliente where srl_idpersona="+ objClienteBE.getSrlIdpersona()+")";

                SQLQuery sqlquery = session.createSQLQuery(consulta);
                sqlquery.addEntity("tipocliente", Tipocliente.class);

                session.flush();
                lista = sqlquery.list();
        } catch (Exception e) {
            e.printStackTrace();
            if(tx!=null)
            {
            tx.rollback();
            }
        }
        return lista.get(0);
    }

}
